using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace trainstorm.BaseClass
{
	public abstract class BaseScreen
	{
		protected Texture2D[] layers;
		protected Vector2[] layerPos;
		protected Vector2[] layerVel;
		protected bool splitScreen;
		protected Game1 game;

		public BaseScreen() { }
		public BaseScreen(Game1 game1)
		{
			game = game1;
		}
		//buat code-reusability nantinya
		public Vector2 getLayerPos(int i)
		{
		    return layerPos[i];
		}
		public Vector2 getLayerVel(int i)
		{
		    return layerVel[i];
		}
		public Texture2D getLayer(int i)
		{
		    return layers[i];
		}
		public virtual void Draw(SpriteBatch batch)
		{
		    for (int i = 0; i < layers.Length; i++)
		    {
		        if (splitScreen)
		            splitDraw(batch, layers[i], layerPos[i]);
		        else
		        {
		            batch.Draw(layers[i], layerPos[i], Color.White);
		            if (layerPos[i].X + layers[i].Width < Game1.screenWidth)
		                batch.Draw(layers[i], new Vector2(layerPos[i].X + layers[i].Width - 2, layerPos[i].Y), Color.White);
		        }
		    }
		}
		public virtual void Update(Game1 game, GameTime gameTime)
		{
		    for (int i = 0; i < layers.Length; i++)
		    {
		        if (!layerVel[i].Equals(Vector2.Zero))
		        {
		            layerPos[i].X += layerVel[i].X * (float)gameTime.ElapsedGameTime.TotalSeconds;
		            layerPos[i].X %= -(layers[i].Width);
		        }
		    }
		}
		protected void splitDraw(SpriteBatch batch, Texture2D image, Vector2 pos)
		{
			if (pos.X + image.Width <= Game1.screenWidth)
			{
				batch.Draw(image, new Rectangle(0, 0, (int)pos.X + image.Width, Game1.screenHeight / 2),
					new Rectangle((int)-pos.X, 0, (int)pos.X + image.Width, Game1.screenHeight / 2), Color.White);
				//image Pad
				batch.Draw(image,
					new Rectangle((int)pos.X + image.Width - 2, 0, Game1.screenWidth - ((int)pos.X + image.Width) + 2, Game1.screenHeight / 2),
					new Rectangle(0, 0, Game1.screenWidth - ((int)pos.X + image.Width), Game1.screenHeight / 2), Color.White);
				batch.Draw(image, new Rectangle(0, Game1.screenHeight / 2, Game1.screenWidth, Game1.screenHeight / 2),
					new Rectangle(Game1.screenWidth - ((int)pos.X + image.Width), 0, Game1.screenWidth, Game1.screenHeight / 2), Color.White);
			}
			else if (pos.X + image.Width <= Game1.screenWidth * 2)
			{
				batch.Draw(image, new Rectangle(0, 0, Game1.screenWidth, Game1.screenHeight / 2),
					new Rectangle((int)-pos.X, 0, Game1.screenWidth, Game1.screenHeight / 2), Color.White);
				batch.Draw(image, new Rectangle(0, Game1.screenHeight / 2, (int)pos.X + image.Width, Game1.screenHeight / 2),
					new Rectangle((int)-pos.X + Game1.screenWidth, 0, (int)pos.X + image.Width, Game1.screenHeight / 2), Color.White);
				//image Pad
				batch.Draw(image,
					new Rectangle((int)pos.X + image.Width - Game1.screenWidth - 2, Game1.screenHeight / 2,
						Game1.screenWidth - (int)pos.X + image.Width, Game1.screenHeight / 2),
					new Rectangle(0, 0,
						Game1.screenWidth - (int)pos.X + image.Width, Game1.screenHeight / 2), Color.White);
			}
			else
			{
				batch.Draw(image, new Rectangle(0, 0, Game1.screenWidth, Game1.screenHeight / 2),
					new Rectangle((int)-pos.X, 0, Game1.screenWidth, Game1.screenHeight / 2), Color.White);
				batch.Draw(image, new Rectangle(0, Game1.screenHeight / 2, Game1.screenWidth, Game1.screenHeight / 2),
					new Rectangle((int)-pos.X + Game1.screenWidth, 0, Game1.screenWidth, Game1.screenHeight / 2), Color.White);
			}
		}
	}
}
